package br.com.nooclix.salon.controller;

import java.util.Calendar;

import br.com.nooclix.salon.application.AttendanceSheetApplication;
import br.com.nooclix.salon.controller.util.ResponseData;
import br.com.nooclix.salon.domain.attendance.Attendance;
import br.com.nooclix.salon.domain.attendance.AttendanceSheet;
import br.com.nooclix.salon.domain.customer.Customer;
import br.com.nooclix.salon.infrastructure.util.ApplicationContextRegister;

public class AttendanceController {
	private AttendanceSheetApplication attendanceSheetRepositoryImpl = ApplicationContextRegister
		.getBean("attendanceSheetApplication", AttendanceSheetApplication.class);
	
	public ResponseData persist(AttendanceSheet attendanceSheet){
		ResponseData responseData = new ResponseData();
		
		try{
			//Setando os valores como null para que o banco possa gerar as chaves
			attendanceSheet.setCode(null);
			for(int i=0; i<attendanceSheet.getAttendance().size(); i++){
				attendanceSheet.getAttendance().get(i).setCode(null);
			}
			attendanceSheetRepositoryImpl.persist(attendanceSheet);
			responseData.setStatus(ResponseData.OK);
		}catch (Exception e) {
			e.printStackTrace();
			responseData.setMessage(e.getMessage());
			responseData.setStatus(ResponseData.FAILED);
		}
		return responseData;
		
	}
	
	public ResponseData alterAttendanceSheet(AttendanceSheet attendanceSheet){
		ResponseData responseData = new ResponseData();
		try{
			for(Attendance att : attendanceSheet.getAttendance()){
				if(att.getCode() == 0){
					att.setCode(null);
				}
			}
			attendanceSheetRepositoryImpl.update(attendanceSheet);
			responseData.setStatus(ResponseData.OK);
		}catch (Exception e) {
			e.printStackTrace();
			responseData.setMessage(e.getMessage());
			responseData.setStatus(ResponseData.FAILED);
		}
		return responseData;
	}
	public ResponseData findBookingByCriterion(Customer customer, Calendar start, Calendar end){
		ResponseData responseData = new ResponseData();
		try{
			responseData.setItems(attendanceSheetRepositoryImpl.findBookingByCriterion(customer, start, end));
			responseData.setStatus(ResponseData.OK);
		}catch(Exception e){
			e.printStackTrace();
			responseData.setMessage(e.getMessage());
			responseData.setStatus(ResponseData.FAILED);
		}
		return responseData;
		
	}
	
	public ResponseData removeAttendanceSheet(AttendanceSheet attendanceSheet){
		ResponseData responseData = new ResponseData();
		try{
			attendanceSheetRepositoryImpl.delete(attendanceSheet);
			responseData.setStatus(ResponseData.OK);
		}catch (Exception e){
			e.printStackTrace();
			responseData.setMessage(e.getMessage());
			responseData.setStatus(ResponseData.FAILED);
		}
		return responseData;
		
	}
	
	public ResponseData findBookingByStatusController(Customer customer, Calendar start, Calendar end, String status){
		ResponseData responseData = new ResponseData();
		try{
			responseData.setItems(attendanceSheetRepositoryImpl.findBookingByStatus(customer, start, end, status));
			responseData.setStatus(ResponseData.OK);
		}catch (Exception e){
			e.printStackTrace();
			responseData.setMessage(e.getMessage());
			responseData.setStatus(ResponseData.FAILED);
		}
		return responseData;
		
	}
	public ResponseData update(AttendanceSheet attendanceSheet){
		ResponseData responseData = new ResponseData();
		try{
			attendanceSheetRepositoryImpl.update(attendanceSheet);
			responseData.setStatus(ResponseData.OK);
		}catch (Exception e) {
			e.printStackTrace();
			responseData.setMessage(e.getMessage());
			responseData.setStatus(ResponseData.FAILED);
		}
		return responseData;
		
	}
	
	
	public ResponseData listBookingsByDay(Calendar date){
		ResponseData responseData = new ResponseData();
		try{
			responseData.setItems(attendanceSheetRepositoryImpl.listBookingsByDay(date));
			responseData.setStatus(ResponseData.OK);
		}catch (Exception e) {
			e.printStackTrace();
			responseData.setMessage(e.getMessage());
			responseData.setStatus(ResponseData.FAILED);
		}
		return responseData;
		
	
}
	public ResponseData findBusyEmployee(Calendar date){
		ResponseData responseData = new ResponseData();
		try{
			System.out.println("Chegou aki: "+ date);
			responseData.setItems(attendanceSheetRepositoryImpl.findBusyEmployee(date));
			responseData.setStatus(ResponseData.OK);
		}catch (Exception e) {
			e.printStackTrace();
			responseData.setMessage(e.getMessage());
			responseData.setStatus(ResponseData.FAILED);
		}
		return responseData;
	}
}
